package com.example.geckohurricanes;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

    public class UserPageActivity extends Activity {

   	 private String uname, pword, ret;
   	 private int admin;
   	 private EditText loginname, loginpass;
   	 DatabaseHandler db;
   	 Contact c, changer;
   	 List<Contact> contacts;
   	
   	 @Override
   	 protected void onCreate(Bundle savedInstanceState) {
   		 super.onCreate(savedInstanceState);
   		 setContentView(R.layout.userpage);
   		 
   		 loginname = (EditText) findViewById(R.id.uname);
   		 loginpass = (EditText) findViewById(R.id.pword);
   		 
   		 db = new DatabaseHandler(this);
   		 contacts = db.getAllContacts();
   		 
   		 Button demote = (Button) findViewById(R.id.btndemote);
   		 Button create = (Button) findViewById(R.id.creategod);
   		 Button unlock = (Button) findViewById(R.id.release);
   		 Button remove = (Button) findViewById(R.id.ban);
   		 Button logout = (Button) findViewById(R.id.btnback);
   		 
   		 demote.setOnClickListener(new View.OnClickListener() {
       		 public void onClick(View v){
       			 uname = loginname.getText().toString();
   				 pword = loginpass.getText().toString();
       			 c = new Contact(uname, pword);
       			 
       			 for (Contact cn : contacts) {
       				 if (cn.equals(c)) {
						changer = cn;
						break;
					}
       			 }
       			 changer.setAdmin(0);
       			Toast.makeText(getApplicationContext(), "Admin Demoted", Toast.LENGTH_SHORT).show();
       		 }
        	});
   		 
   		 create.setOnClickListener(new View.OnClickListener() {
   			 public void onClick(View v){
   				 uname = loginname.getText().toString();
   				 pword = loginpass.getText().toString();
   				 c = new Contact(uname, pword);
   				 for (Contact cn : contacts) {
      				 if (cn.equals(c)) {
						changer = cn;
						break;
					}
      			 }
      			 changer.setAdmin(1);
      			Toast.makeText(getApplicationContext(), "Admin Created", Toast.LENGTH_SHORT).show();
   			 }
   		 });
   		 
   		 unlock.setOnClickListener(new View.OnClickListener() {
   			 public void onClick(View v){
   				uname = loginname.getText().toString();
  				pword = loginpass.getText().toString();
  				db.addContact(new Contact(uname, pword));
   				Toast.makeText(getApplicationContext(), "User Created.", Toast.LENGTH_SHORT).show();
   			 }
   		 });
   		 
   		 remove.setOnClickListener(new View.OnClickListener(){
   			 public void onClick(View v){
   				 uname = loginname.getText().toString();
   				 pword = loginpass.getText().toString();
   				 
   				 c = new Contact(uname, pword);
  				 for (Contact cn : contacts) {
     				 if (cn.equals(c)) {
						changer = cn;
						break;
					}
     			 }
   				 db.deleteContact(changer);
   				Toast.makeText(getApplicationContext(), "User Deleted", Toast.LENGTH_SHORT).show();
   			 }
   		 });
   		 
   		//Logout
   		 logout.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				db.close();
				finish();
			}
   		 });
   		 
   	 }   	 

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.user_page, menu);
		return true;
	}

}
